package com.ewhale.feitengguest.ui.user.task.adapter;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.TaskDetailsDto;
import com.ewhale.feitengguest.ui.user.mine.adapter.ShowImageAdapter;
import com.simga.library.adapter.recyclerview.BaseViewHolder;
import com.simga.library.adapter.recyclerview.MutiRecyclerAdapter;
import com.simga.library.adapter.recyclerview.OnItemListener;

import java.util.List;

import butterknife.BindView;
import cc.shinichi.library.ImagePreview;

public class StepListAdapter extends MutiRecyclerAdapter<TaskDetailsDto.MissionStepDtosBean> {

    private ShowImageAdapter mImageAdaoter;

    public StepListAdapter(List<TaskDetailsDto.MissionStepDtosBean> data) {
        super(data, R.layout.item_step);
    }

    @Override
    protected BaseViewHolder<TaskDetailsDto.MissionStepDtosBean> getViewHolder() {
        return new ViewHolder(view);
    }

    class ViewHolder extends BaseViewHolder<TaskDetailsDto.MissionStepDtosBean> {
        @BindView(R.id.tv_step)
        TextView mTvStep;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.nv_image)
        RecyclerView mNvImage;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void build(TaskDetailsDto.MissionStepDtosBean object, int position) {
            mTvStep.setText(String.valueOf(object.getNum()));
            mTvContent.setText(object.getExplain());
            GridLayoutManager manager = new GridLayoutManager(mContext, 3);
            mNvImage.setLayoutManager(manager);
            mImageAdaoter = new ShowImageAdapter(object.getImgUrlList(), true);
            mNvImage.setAdapter(mImageAdaoter);

            mTvContent.setOnLongClickListener(v -> {
                if (listener != null) {
                    listener.onClick(object.getExplain());
                }
                return false;
            });

            mImageAdaoter.setOnItemClickListener((view, position1) ->
                    ImagePreview.getInstance().setContext(mContext)
                            .setIndex(position1)
                            .setShowDownButton(false)
                            .setImageList(object.getImgUrlList())
                            .setLoadStrategy(ImagePreview.LoadStrategy.AlwaysOrigin)
                            .setZoomTransitionDuration(300)
                            .setEnableDragClose(true)
                            .setErrorPlaceHolder(R.drawable.default_image).start());
        }
    }

    private onLongClickListener listener;

    public void setListener(onLongClickListener listener) {
        this.listener = listener;
    }

    public interface onLongClickListener {
        void onClick(String content);
    }
}
